package com.rg1906.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.rg1906.service.impl.InsertStudentMessageServiceImpl;


@WebServlet("/InsertStudentMessage")
public class InsertStudentMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	InsertStudentMessageServiceImpl service=new InsertStudentMessageServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String realname = request.getParameter("realname");
		String gid = request.getParameter("gid");//gid为年级名
		String cid=request.getParameter("cid");//cid 为班级名
		String phone=request.getParameter("phone");
		String address=request.getParameter("address");
		String sex=request.getParameter("sex");
		String age=request.getParameter("age"); 
		String img="static/images/4.jpg";
		try {
			String MaxNum=null;
			String SNum=null;
			int ssNum=0;
			String sNum=null;
			MaxNum = service.getMaxNum(gid,cid);//最大学号
			int cid2 = service.getCid(cid);
			int gid2 = service.getGid(gid);
			if(MaxNum==null) {
				MaxNum="S"+gid2+0+0+0+cid2+1;
				System.out.println(MaxNum);
				String[] split = MaxNum.split("S");
				 SNum = split[1];//分割得来的学号加1
				System.out.println(SNum);
				 ssNum = Integer.parseInt(SNum);
				 sNum="S"+ssNum;//学号
				System.out.println(sNum);
			}else {
				System.out.println(MaxNum);
				String[] split = MaxNum.split("S");
				 SNum = split[1];//分割得来的学号加1
				System.out.println(SNum);
				 ssNum = Integer.parseInt(SNum)+1;
				 sNum="S"+ssNum;//学号
				System.out.println(sNum);
			}
			int gradeCid = service.getCid(cid);//获取年级id
			int gradeGid = service.getGid(gid);//获取班级id
			int i = service.insertStudent(realname,gradeCid,gradeGid,sNum,phone,address,sex,age);

//			//添加学生人员管理
//			
			int ii = service.insertUser(sNum,img);//将学号作为登录默认账号
			int userId = service.getUserId(sNum);//查找user表中的id 为t_student表中的uid
			int iii = service.InsertStudentUid(userId,realname);//查到的id为student表中的uid
			int iiii = service.insertTScore(sNum);
			if(i>0&&ii>0&&iii>0&&iiii>0) {
				response.getWriter().print(true);
			}else {
				response.getWriter().print(false);
			}
			
			
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
